package keye.document_manage.util;

public class ConvertStringUtil {

	/**
	 * 将b转换为显示的GB、MB、KB等
	 * @param size 单位B
	 * @return
	 */
	public static String convertFileSize(double size) {
        long kb = 1024;
        long mb = kb * 1024;
        long gb = mb * 1024;
 
        if (size >= gb) {
            return String.format("%.1f GB", (double) size / gb);
        } else if (size >= mb) {
        	double f = size / mb;
            return String.format(f > 100 ? "%.0f MB" : "%.1f MB", f);
        } else if (size >= kb) {
        	double f = size / kb;
            return String.format(f > 100 ? "%.0f KB" : "%.1f KB", f);
        } else
            return String.format("%.0f B", size);
    }
	
	/**
	 * 将ms转换为显示的s、m、h等
	 * @param time 单位ms
	 * @return
	 */
	public static String convertTime(double time) {
        long s = 1000;
        long m = s * 60;
        long h = m * 60;
        long day = h * 24;
 
        if (time >= day) {
            return String.format("%.1f day", (double) time / day);
        } else if (time >= h) {
        	double f = time / h;
            return String.format(f > 12 ? "%.0f h" : "%.1f h", f);
        } else if (time >= m) {
        	double f = time / m;
            return String.format(f > 30 ? "%.0f min" : "%.1f min", f);
        } else if(time >=s){
        	double f = time / s;
            return String.format(f > 30 ? "%.0f s" : "%.1f s", f);
        } else
            return String.format("%.0f ms", time);
    }
	
	public static void main(String[] args) {
		System.out.println(convertTime(1000*25.6));
	}
}
